/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package embuzzled.ui;

import embuzzled.play.*;
import embuzzled.ui.*;
import embuzzled.play.CSVWriter;
import embuzzled.play.CSVReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author Satyajeet
 */
public class GridIOManager {

    public ArrayList<String> getnames()
    {
        // Basic declaration to store gridnames from files
        ArrayList<String> Gridnames = new ArrayList<String>();
        // Let us now query the current dir for .grid files
        //File currdir = new File(".");
        //System.out.println(currdir.getAbsolutePath());
        
        
        File thisdir = new File("./Gridbox/");
        String[] AllFiles = thisdir.list();
        for(int loop=0;loop<AllFiles.length;loop++)
        {
            //System.out.println("Filename:" + AllFiles[loop]);
            int length = AllFiles[loop].length();
            if(length > 4)
            {
                String last4 = AllFiles[loop].substring(length-4,length);
                //System.out.println("Last4 = " + last4);
                if(last4.equalsIgnoreCase("grid"))
                {
                    //System.out.println("This file contains a grid > " + AllFiles[loop]);
                    Gridnames.add(AllFiles[loop]);
                }
            }
        }

        return Gridnames;
    }


    public void saveGridtoFile(int rows, int cols, GCell[][] Griddata, String Gridname) throws IOException
    {
        CSVWriter csvw = new CSVWriter(new FileWriter("./Gridbox/"+Gridname+".grid"),',');
        String[] line = new String[2];

        line[0] = Integer.toString(rows);
        line[1] = Integer.toString(cols);

        csvw.writeNext(line);

        String[] dataline = new String[cols];

        for(int loopr=0;loopr<rows;loopr++)
        {
            dataline = null;
            dataline = new String[cols];
            for(int loopc=0;loopc<cols;loopc++)
            {
                dataline[loopc] = Boolean.toString(Griddata[loopr][loopc].GBool);

            }
            csvw.writeNext(dataline);
        }



        csvw.close();
        

    }

    public Grid readGridfromFile(String filename) throws FileNotFoundException, IOException
    {
        CSVReader csvr = new CSVReader(new FileReader("./Gridbox/"+filename));
        String[] nextLine;
        int rows, cols = 0;
        int count = 0;
        Grid mygrid = null;
         while ((nextLine = csvr.readNext()) != null) {
        // nextLine[] is an array of values from the line
        //System.out.println(nextLine[0] + nextLine[1] + "etc...");

             if(count == 0)
             {
                 rows = Integer.parseInt(nextLine[0]);
                 cols = Integer.parseInt(nextLine[1]);
                 mygrid = new Grid(rows, cols);
                 count++;
             }
             else
             {
                 for(int loopc=0;loopc<cols;loopc++)
                 {
                    mygrid.datagrid[(count-1)][loopc] = Boolean.parseBoolean(nextLine[loopc]);

                 }
                 count++;
             }



        }
        //System.out.println("Grid returned");
        return mygrid;

    }


}
